/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
var $alert = require('libs/alert');
var $confirm = require('libs/confirm');
require('easyui');
var isLocked={"0":"否","1":"是"};
var isSale = require('libs/user-type').isSale;
var clickEvent = {
    modifyInit:function () {
        $("body").on("click","a[name=modify]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="user-update?id="+id;
        });
        return clickEvent;
    },
    lockInit:function () {
        $("body").on("click","a[name=lock]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认锁定该用户吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/user/operate",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id,
                        "accountNonLocked":"1"
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("锁定成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    unLockInit:function () {
        $("body").on("click","a[name=unLock]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认解锁该用户吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/user/operate",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id,
                        "accountNonLocked":"0"
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("解锁成功!");
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    pwdResetInit:function () {
        $("body").on("click","a[name=pwdReset]",function () {
            var id = $(this).parents("tr").children(":first").text();
            $confirm("确认重置该用户的密码吗?",function () {
                $.ajax({
                    url:"api/v1/p/mms/user/resetPwd",
                    type:"post",
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    data:JSON.stringify({
                        "id":id
                    }),
                    success:function (result) {
                        if(result.code=="200") {
                            $alert("重置成功!新密码为"+result.data);
                            $("#main_table").datagrid("reload");
                        }else{
                            $alert(result.message);
                        }
                    }
                })
            })
        });
        return clickEvent;
    },
    moduleConfigInit:function () {
        $("body").on("click","a[name=moduleConfig]",function () {
            var id = $(this).parents("tr").children(":first").text();
            var systemId = $(this).parents("tr").children(":eq(4)").text();
            window.location.href="user-module-config?id="+id+"&&systemId="+systemId;
        });
        return clickEvent;
    },
    searchInit:function () {
        $("#a_search").on("click",function () {
            $("#main_table").datagrid('load', {
                warehouseId: $('#select_warehouse').combobox('getValue'),
                systemId: $('#select_system').combobox('getValue'),
                accountNonLocked: $('#select_lock').datebox('getValue'),
                username: $('#tb input[name=username]').val()
            })
        });
        return clickEvent;
    },
    detailInit:function () {
        $("body").on("click","a[name=username]",function () {
            var id = $(this).parents("tr").children(":first").text();
            window.location.href="user-detail?id="+id;
        });
        return clickEvent;
    }
};
$(function(){
    pageInit();
    mainTableInit();
});
function pageInit(){
    $("#a_create").show();
    clickEvent.modifyInit().lockInit().unLockInit().pwdResetInit().moduleConfigInit().searchInit().detailInit();
    warehouseInit();
}

function warehouseInit() {
    $("#select_warehouse").combobox({
        url:'api/v1/p/crm/warehouse/query',
        valueField:'warehouseId',
        textField:'warehouseName',
        queryParams:{
            warehouseType:"DOME"
        },
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift({warehouseId:'',warehouseName:'全部'});
                return result.data;
            }
        }
    })
}
function mainTableInit(){
    $("#main_table").datagrid({
        url:'api/v1/p/mms/user/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        emptyMsg : "暂无数据",
        selectOnCheck : true,
        singleSelect : true,
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                hidden:true
            },{
                field:'username',
                title:'用户名',
                width:150,
                formatter:function (value,row,index) {
                    return "<a href='javascript:;' name='username'>"+value+"</a>"
                }
            },{
                field:'name',
                title:'用户姓名',
                width:100
            },{
                field:'lastLogin',
                title:'最后登录日期',
                width:200
            },{
                field:'systemId',
                hidden:true
            },{
                field:'systemName',
                title:'所属系统',
                width:100
            },{
                field:'warehouseName',
                title:'所属仓库',
                width:150
            },{
                field:'accountNonLocked',
                title:'是否锁定',
                width:100,
                formatter:function (value,row,index) {
                    return isLocked[value];
                }
            },{
                field:'opertation',
                title:'操作',
                hidden:isSale,
                width:300,
                formatter:function (value,row,index) {
                    if(row.accountNonLocked=="0"){
                        return "<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='lock'>锁定</a>|<a href='javascript:;' name='pwdReset'>重置密码</a>|<a href='javascript:;' name='moduleConfig'>配置模块</a>";
                    }else{
                        return "<a href='javascript:;' name='modify'>修改</a>|<a href='javascript:;' name='unLock'>解锁</a>|<a href='javascript:;' name='pwdReset'>重置密码</a>|<a href='javascript:;' name='moduleConfig'>配置模块</a>";
                    }
                }
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
}